package com.greedy.bulls.stock.user.config;

import com.greedy.bulls.stock.common.generator.GlobalIdGenerator;
import com.greedy.bulls.stock.common.utils.GlobalConstants;
import jdk.nashorn.internal.parser.TokenStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-08-05
 * Description: 全局系统唯一ID生成器
 */
@Configuration
public class GlobalSystemConfiguration {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * snowflake算法的工作中心ID
     */
    @Value("${snowflake.workerId}")
    private Long workerId;

    /**
     * snowflake算法的数据中心ID
     */
    @Value("${snowflake.dataCenterId}")
    private Long dataCenterId;



    /**
     * 全局唯一ID生成器
     *
     * @return 全局唯一ID
     */
    @Bean
    public GlobalIdGenerator globalIdGenerator() {
        if (null == workerId || null == dataCenterId) {
            return new GlobalIdGenerator();
        }
        return new GlobalIdGenerator(workerId, dataCenterId);
    }

    /**
     * tokenStore配置
     * @return tokenStore
     */
    @Bean
    public TokenStore tokenStore() {
        RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
        tokenStore.setPrefix(GlobalConstants.OAUTH_PREFIX_KEY);
        return tokenStore;
    }

}
